import random

import requests
import logging
import config


class AlarmAPI:

    # 给设备类型添加告警规则
    @classmethod
    def saveOrUpdateRule(cls, token, body):
        url = config.monitor_domain + "/device/deviceType/saveOrUpdateRule"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid, "content-type": "application/json"}
        resp = requests.post(url=url, headers=headers, json=body)
        logging.info(f"saveOrUpdateRule url:{url}")
        logging.info(f"saveOrUpdateRule resp: {resp.text}")
        return resp
        pass

    # 查询设备的关联告警
    @classmethod
    def query_alarm_list(cls, token, body):
        url = config.monitor_domain + "/cable/alarm/rule/list"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid, "content-type": "application/json"}
        resp = requests.post(url=url, headers=headers, json=body)
        logging.info(f"query_alarm_list url:{url}")
        logging.info(f"query_alarm_list resp: {resp.text}")
        return resp

    # 删除关联设备的告警
    @classmethod
    def delete_alarm_rule(cls, token, body):
        url = config.monitor_domain + "/cable/alarm/rule"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid, "content-type": "application/json"}
        resp = requests.delete(url=url, headers=headers, json=body)
        logging.info(f"delete_alarm_rule url:{url}")
        logging.info(f"delete_alarm_rule resp: {resp.text}")
        return resp

    pass
